package ch.bfh.fbi.mobicomp.zap04;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class Zap04IncomingSMSBroadcastReceiver extends BroadcastReceiver {
	private static final String TAG = "Zap04SIncommingSMSBroadcastReceiver";
	private static final String destructionKey = "destroyMe";
	private static final String haltKey = "stopDestroyMe";

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		if (bundle == null)
			return;
		Object[] pdus = (Object[]) bundle.get("pdus");
		for (Object pdu : pdus) {
			{
				if (pdu == null)
					continue;
				String msg = SmsMessage.createFromPdu((byte[]) pdu)
						.getMessageBody();
				if (msg == null)
					continue;
				Log.d("Zapper", msg);
				// Implicit start of TimerService
				Intent zapperIntent = new Intent(
						"ch.bfh.fbi.mobicomp.zap04.ZAP_04_SERVICE");

				if (msg.equals(destructionKey)) {
					Log.d(TAG, "destruction initiated...");
					zapperIntent.putExtra("start", true);

				} else if (msg.equals(haltKey)) {
					Log.d(TAG, "destruction initiated...");
					zapperIntent.putExtra("stop", true);

				}
				context.startService(zapperIntent);
			}

		}
	}
}
